define(['jquery', 'bootstrap', 'backend', 'table', 'form','layer'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'region/index' + location.search,
                    // add_url: 'region/add',
                    edit_url: 'region/edit',
                    del_url: 'region/del',
                    get_name_url: 'region/get_name',
                    multi_url: 'region/multi',
                    import_url: 'region/import',
                    table: 'region',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'code',
                sortName: 'sort',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'code', title: __('Code')},
                        {field: 'name', title: '城市名称', operate: 'LIKE'},
                        // {field: 'level', title: '级别', operate: 'LIKE'},
                        // {field: 'pcode', title: __('Pcode')},
                        // {field: 'pname', title: __('Pname'), operate: 'LIKE'},
                        // {field: 'fullname', title: __('Fullname'), operate: 'LIKE'},
                        {field: 'longitude', title: __('Longitude'), operate:'BETWEEN'},
                        {field: 'latitude', title: __('Latitude'), operate:'BETWEEN'},
                        {
                            field: 'sort',
                            title: '排序',
                            formatter: function (value, row, index) {
                                return '<input type="text" class="form-control text-center text-weigh" data-id="' + row.id + '" value="' + value + '" style="width:50px;margin:0 auto;" />';
                            },
                            events: {
                                "dblclick .text-weigh": function (e) {
                                    e.preventDefault();
                                    e.stopPropagation();
                                    return false;
                                }
                            }
                        },
                        {field: 'first_name', title: __('First_name')},
                        {field: 'hot', title: __('Hot'),searchList: {"0":'普通',"1":'热门'}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $(document).on("change", ".text-weigh", function () {
                $(this).data("params", {weigh: $(this).val()});
                Table.api.multi('', [$(this).data("id")], table, this);
                return false;
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            //胜诉
            $(".btn-process-1").click(function(){
                // var ids = Table.api.selectedids(table);
                layer.confirm('确定要更新数据吗?', {btn: ['是','否'] },
                    function(index){
                        layer.close(index);
                        $.post("region/get_name", {},function(response){
                            if(response.code == 1){
                                Toastr.success(response.msg);
                                $(".btn-refresh").trigger('click');
                            }else{
                                Toastr.error(response.msg);
                            }
                        }, 'json');
                    },
                    function(index){
                        layer.close(index);
                    }
                );
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {

            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
